﻿using Dycox.Collections;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dycox.ComponentModel
{
    public abstract class ViewObject
    {
        protected ViewObject(string name)
        {
            if (name == null)
                throw new ArgumentNullException("name");

            _name = name;

        }

        private string _name;

        public string Name
        {
            get { return _name; }
        }

        private ViewGroup _parent;

        public ViewGroup Parent
        {
            get { return _parent; }
        }

        internal void DeattachFromParent()
        {
            _parent = null;
        }

        public ModelView View
        {
            get
            {
                if (this is ModelView)
                    return (ModelView)this;
                else if (_parent != null)
                    return _parent.View;
                else
                    return null;
            }
        }

        public class Collection : OwnedCollection<ViewObject, ViewGroup>
        {
            internal Collection(ViewGroup parent)
                : base(parent)
            {

            }

            protected override void SetItemOwner(ViewObject item, ViewGroup owner)
            {
                item._parent = owner;
            }

            protected override ViewGroup GetItemOwner(ViewObject item)
            {
                return item._parent;
            }
        }
    }
}
